﻿Imports System.Data.SqlClient
Public Class Frm_Av_Enemi
    

    Public Sub remplirMo7ami(ByVal comb As ComboBox)
        Partager.moi.remplir_List_avocat_enemie()

        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each clt As Avocat_enemie In Partager.moi.List_avocat_enemie
            str = clt.Nom_a & "  " & clt.Prenom_a
            htClient.Add(clt.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            comb.Items.Add(ImageType)
        Next
        comb.DisplayMember = "value"
        comb.ValueMember = "key"
        If comb.Items.Count > 0 Then
            comb.SelectedIndex = 0
        End If
    End Sub
    Public Sub Replirlistmohami()
        List_mohami.Items.Clear()
        Partager.dv.remplir_List_avocat_enemie()
        For Each khib As Dossier_Avocat_Adversaire In Partager.dv.List_avocat_enemie
            For Each clt As Avocat_enemie In Partager.moi.List_avocat_enemie
                If khib.Id_Avocat_Adversaire = clt.Id Then

                    Partager.line = New System.Windows.Forms.ListViewItem(New String() {"", clt.Num_prof, clt.Nom_a, clt.Prenom_a, clt.Id}, -1)
                    'il est indispensable d'ajouter chekHand false et true
                    Partager.checkHand = False
                    If khib.nomme Then
                        Partager.line.Checked = True
                    Else
                        Partager.line.Checked = False
                    End If
                    Me.List_mohami.Items.AddRange(New System.Windows.Forms.ListViewItem() {Partager.line})
                    Partager.checkHand = True
                End If
            Next
        Next

    End Sub


    Private Sub Btn_parc_num_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_num.Click
        Me.Enabled = False
        Partager.rech = True
        Partager.type_rech_Avo_adver = Me.Name
        Frm_Rech_Avo_Adver.Show()
    End Sub


    Private Sub List_mohami_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_mohami.SelectedIndexChanged
        For Each i As Integer In List_mohami.SelectedIndices
            Partager.idAvoDoss = CInt(List_mohami.SelectedItems(0).SubItems(4).Text)
            Partager.nomMohami = List_mohami.SelectedItems(0).SubItems(2).Text & " " & List_mohami.SelectedItems(0).SubItems(3).Text
        Next
    End Sub

    Private Sub List_mohami_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles List_mohami.ItemChecked
        'Dim Partager.dossAvoEne As dossAvoBur = dv.List_avocat_bureau(List_mowakilin.SelectedIndices.Item(0))
        If Partager.checkHand Then
            Dim i As Integer = 0
            For Each item1 As ListViewItem In List_mohami.Items
                Partager.dossAvoEne = New Dossier_Avocat_Adversaire
                Partager.dossAvoEne = Partager.dv.List_avocat_enemie(i)
                Partager.dossAvoEne.nomme = item1.Checked
                Partager.dv.modifierAvoEnemNomme(Partager.dossAvoEne)
                i += 1
            Next
        End If
    End Sub
    Private Sub Btn_ajout_mohami_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout_mohami.Click
       
            If CombNomAv.Text <> "" Then

                Try
                    If MsgBox("هل تريد فعلا إضافة هذا المحامي لهذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                        Partager.close()
                        Partager.deClt = DirectCast(CombNomAv.SelectedItem, DictionaryEntry)
                        Dim idmohami As Integer = CInt(Partager.deClt.Key)


                        Partager.dossAvoEne = New Dossier_Avocat_Adversaire(idmohami, True)
                        Partager.dv.Ajouter_avocat_enemie(Partager.dossAvoEne)

                        Replirlistmohami()

                        MsgBox("تمت إضافة المحامي للملف بنجاح", MsgBoxStyle.Information, "نجاح")
                    End If
                Catch sqlEx As SqlException
                    If sqlEx.Message.Contains("PK_Dossier_Avocat_Adversaire") Then
                        MsgBox("عفوا، هذا المحامي تمت إضافته لهذاالملف من قبل", MsgBoxStyle.Critical, "خطأ")
                    Else
                        MsgBox(sqlEx.Message)
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    End Sub
    Private Sub Btn_supp_mohami_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp_mohami.Click
       If List_mohami.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المحامي من هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim cltDoss As Dossier_Avocat_Adversaire = Partager.dv.List_avocat_enemie(List_mohami.SelectedIndices.Item(0))
                    Partager.dv.suprimmer_avocat_enemie(cltDoss)

                    Replirlistmohami()

                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر محامي من القائمة", MsgBoxStyle.Information, "خطأ")
        End If

    End Sub

    Private Sub Btn_mokhabara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_mokhabara.Click
        If Frm_Idafa_Mowakilin.Comb_marji3ona.SelectedIndex >= 0 And List_mohami.SelectedIndices.Count > 0 Then
            Frm_Mohami_mokhabara.MdiParent = Frm_Main
            Me.Enabled = False
            Frm_Mohami_mokhabara.Show()
        Else
            MsgBox("يجب عليك اختيار الملف و المحامي معا", MsgBoxStyle.Critical, "خطأ")
        End If
    End Sub

    Private Sub Frm_Av_Mowakil_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Frm_Idafa_Mowakilin.Enabled = True
    End Sub

    Private Sub Frm_Av_Enemi_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        List_mohami.Columns(0).DisplayIndex = List_mohami.Columns.Count - 1
        remplirMo7ami(CombNomAv)
        Replirlistmohami()
        Me.BringToFront()
    End Sub
End Class